﻿using System;
using Castle.Core.Logging;
using Inovout.Resources;
using Castle.Services.Logging.Log4netIntegration;

namespace Inovout
{
    /// <summary>
    /// 日志
    /// </summary>
    public static class Logger
    {
        static Logger()
        {
            ConfigFile log4netConfigFile = new ConfigFile("log4net", typeof(Logger));
            ExtendedLog4netFactory loggerFactory = new ExtendedLog4netFactory(log4netConfigFile.Read()); ;
            
            ComponentRegistry.AddComponent<IExtendedLoggerFactory>("iloggerfactory", loggerFactory);
            ComponentRegistry.AddComponent<ILoggerFactory>("iloggerfactory.base", loggerFactory);
        }

        public static ILogger Create(Type type)
        {
            ArgumentAssert.IsNotNull(type, "type");
            ILoggerFactory loggerFactory = ComponentRegistry.Resolve<ILoggerFactory>();
            if (loggerFactory == null)
            {
                loggerFactory = new NullLogFactory();
            }
            return loggerFactory.Create(type);
        }

        public static ILogger Create(string loggerName)
        {
            ArgumentAssert.IsNotDefault(loggerName, "loggerName");
            ILoggerFactory loggerFactory = ComponentRegistry.Resolve<ILoggerFactory>();
            if (loggerFactory == null)
            {
                loggerFactory = new NullLogFactory();
            }
            return loggerFactory.Create(loggerName);
        }
    }
}